import threading
import time
import tkinter as tk


n = 10
event = threading.Event()
global_pause:bool = False

def label_bg_white():
    for i in label_list:
        i.config(bg='white')
# 程序运行
def btn_run_event():
    def run_event(_event:threading.Event):
        for label_i in label_list:
            label_i.config(bg='red')
            root.update()           # 实时更新页面，否则只有当事件全部执行完，才会刷新页面
            if global_pause:
                _event.wait()
            time.sleep(0.2)
    label_bg_white()
    t = threading.Thread(target=run_event,args=(event,))
    t.start()
# 程序停止
def btn_pause_event():
    global global_pause
    global_pause = True     # 程序停止
# 程序再次启动,单步运行
def btn_continue_event():
    global global_pause
    global_pause = False
    event.set()
    event.clear()
# 程序再次启动__单步运行
def btn_single_run_event():
    global global_pause
    # global_pause = False
    event.set()
    event.clear()
root = tk.Tk()
root.geometry('300x300')
btn_frame = tk.Frame(root,height=50)
btn_frame.pack(fill='x')
btn_run = tk.Button(btn_frame, text='运行', command=btn_run_event)
btn_pause = tk.Button(btn_frame, text='暂停运行', command=btn_pause_event)
btn_continue = tk.Button(btn_frame, text='继续运行', command=btn_continue_event)
btn_single_run = tk.Button(btn_frame, text='单步运行', command=btn_single_run_event)
btn_run.pack(side='left')
btn_pause.pack(side='left')
btn_continue.pack(side='left')
btn_single_run.pack(side='left')
label_list = [tk.Label(root,text=f'文本{i}') for i in range(1,n+1)]

print(label_list)
for label in label_list:
    label.pack()
root.mainloop()
